import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Button, Message, ButtonGroup } from 'tinper-bee';
import ApprovalModal from './ApprovalModal';
import mirror, { connect, actions } from "mirrorx";
import Approvalchart from './ApprovalChart';
import model from "./model";

mirror.model(model);
const propTypes = {
    iconType: PropTypes.string,//图标的className，必填
};

const defaultProps = {

};


class Approval extends Component {
    constructor(props) {
        super(props);
        this.state = {

        };
    }
    async componentDidMount() { 
        
    }

    onCloseApproval = (data) => {
        if(data) {
            let {ButtonType} = this.state;
            switch(ButtonType) {
                case "ButtonSubmit": this.OnButtonSubmit(); break;
                case "ButtonCallback": this.OnButtonCallback(); break;
                case "ButtonApprove": this.OnButtonApprove(data); break;
                case "ButtonCancelApprove": this.OnButtonCancelApprove(); break;
            }
        }
        this.setState({
            showApprovalModalModal:false
        })
    }

    OnButtonSubmit = () => {
        if(!this.state.findUniqueObj) return;
        debugger;
        let {ButtonApprovalCal,bpmService,selectData} = this.props;
        let {actProcModelCode,billTypeCode,busiTypeId} = this.state.findUniqueObj;
        let {nodeJs,url} = bpmService["ButtonSubmit"] && bpmService["ButtonSubmit"];
        let param = {
            // data:{
            //     "id":selectData[0].id,
            //     "billTypeId":billTypeCode,
            //     "busiTypeId":busiTypeId,
            //     "personId":selectData[0].applicantId,
            // },
            data:selectData,
            param: {
                billTypeCode:billTypeCode,
                processDefineCode:actProcModelCode,
                nodeJs:nodeJs
            },
            url:url,
            callback:ButtonApprovalCal
        }
        actions.ApprovalModel1.onSubmit(param);
        //ButtonApprovalCal();
        
    }
    OnButtonCallback = () => {
        let {bpmService,ButtonApprovalCal,selectData} = this.props;
        let {url} = bpmService["ButtonCallback"] && bpmService["ButtonCallback"];
        let param = {
            url:url,
            data:selectData,
            //data:{id:selectData[0].id},
            callback:ButtonApprovalCal
        }
        actions.ApprovalModel1.onCallback(param);
        //ButtonApprovalCal();
    }
    OnButtonApprove = (data) => {
        let {bpmService,ButtonApprovalCal} = this.props;
        let {processInstanceId,processDefinitionId,taskId} = this.state.billBpmObj;
        let agreeUrl = bpmService["ButtonApprove"] && bpmService["ButtonApprove"].url;
        let disUrl = bpmService["RejectApprove"] && bpmService["RejectApprove"].url;
        let param;
        if(data.isAgree == "agree") {
            param = {
                url:agreeUrl,
                data:{
                    "taskId": taskId,
                    "processInstanceId": processInstanceId,
                    "processDefinitionId": processDefinitionId,
                    "comment": data.comment,
                    "approvetype": data.isAgree
                },
                callback:ButtonApprovalCal
            }
        }else if(data.isAgree == "rejectToBillMaker") {
            param = {
                url:disUrl,
                data:{
                    "taskId": taskId,
                    "processInstanceId": processInstanceId,
                    "processDefinitionId": processDefinitionId,
                    "comment": data.comment,
                    "activityId": "markerbill",
                    "approvetype": data.isAgree
                },
                callback:ButtonApprovalCal
            }
        }
        
        actions.ApprovalModel1.onApprove(param);
        //ButtonApprovalCal();
    }

    OnButtonCancelApprove = () => {
        let {selectData,bpmService,ButtonApprovalCal} = this.props;
        let {url} = bpmService["ButtonCancelApprove"] && bpmService["ButtonCancelApprove"];
        let param ={
            url:url,
            data:[{billId: selectData[0].id}],
            callback:ButtonApprovalCal
        }
        actions.ApprovalModel1.onCancelApprove(param);
        //ButtonApprovalCal();
    }

    initDisableFlag = () => {
        let { selectData } = this.props;
        let disabledSetsFlag = {
            ButtonSubmit: true,
            ButtonCallback: true,
            ButtonApprove: true,
            ButtonCallBackApprove: true,
            ButtonCancelApprove: true

        };
        if (!selectData || selectData.length !== 1) {
            return disabledSetsFlag;
        }
        // "ButtonSubmit":
        if (selectData[0].state == "0") {
            disabledSetsFlag["ButtonSubmit"] = false;
        }
        // "ButtonCallback":
        if (selectData[0].state == "1") {
            disabledSetsFlag["ButtonCallback"] = false;;
        }
        // "ButtonApprove":
        if (selectData[0].state == "1" || selectData[0].state == "2") {
            disabledSetsFlag["ButtonApprove"] = false;
        }
       
        // "ButtonCallBackApprove":
        if (selectData[0].state == "2" || selectData[0].state == "1") {
            disabledSetsFlag["ButtonCallBackApprove"] = false;
        }
        // "ButtonCancelApprove":
        if (selectData[0].state == "2" || selectData[0].state == "3") {
            disabledSetsFlag["ButtonCancelApprove"] = false;
        }
        return disabledSetsFlag;
    }

    openAlert = (type) => {
        let {selectData} = this.props;
        let {billTypeCode,search_AUTH_APPCODE,tranTypeCode} =  this.props.bpmService["FindUnique"];
        if(type=="ButtonSubmit") {
            let param = {
                callback: (data)=>{
                    this.setState({
                        ButtonType:type,
                        showApprovalModalModal:true,
                        findUniqueObj:data
                    })
                },
                data:{
                    billTypeCode:billTypeCode,
                    tranTypeCode:tranTypeCode,
                    search_AUTH_APPCODE:search_AUTH_APPCODE
                }
            }
            
            actions.ApprovalModel1.getFindUnique(param);
    
        }else  if(type=="ButtonApprove") {
            let param = {
                callback: (data)=>{
                    this.setState({
                        ButtonType:type,
                        showApprovalModalModal:true,
                        billBpmObj:data
                    })
                },
                data:{
                    billId:selectData[0].id
                }
            }
            actions.ApprovalModel1.getBillBpm(param);
        }else {
            this.setState({
                ButtonType:type,
                showApprovalModalModal:true
            })
        }
        
    }

    render() {
        let { showApprovalModalModal,ButtonType } = this.state;
        let {bpmService} = this.props;
        let disabledSetsFlag = this.initDisableFlag() || {};

        return (
            <>
                <ButtonGroup style={{ margin: 15 }}>
                    
                    {bpmService.ButtonSubmit && <Button shape='border' disabled={disabledSetsFlag["ButtonSubmit"]} onClick={() => this.openAlert("ButtonSubmit")} size='sm' >
                        提交
                    </Button>}
                    {bpmService.ButtonCallback && <Button shape='border' disabled={disabledSetsFlag["ButtonCallback"]} onClick={() => this.openAlert("ButtonCallback")} size='sm' >
                        收回
                    </Button>}
                    
                    {bpmService.ButtonApprove && <Button shape='border' disabled={disabledSetsFlag["ButtonApprove"]} onClick={() => this.openAlert("ButtonApprove")} size='sm' >
                        审批
                    </Button>}
                    {bpmService.ButtonCallBackApprove && <Button shape='border' disabled={disabledSetsFlag["ButtonCallBackApprove"]} onClick={this.ButtonCallBackApprove} size='sm' >
                        撤销审批
                    </Button> }
                    {bpmService.ButtonCancelApprove && <Button shape='border' disabled={disabledSetsFlag["ButtonCancelApprove"]} onClick={() => this.openAlert("ButtonCancelApprove")} size='sm' >
                        取消审批
                    </Button>}
                </ButtonGroup>

                <ApprovalModal 
                    ButtonType={ButtonType}
                    showApprovalModalModal={showApprovalModalModal}
                    onCloseApproval = {this.onCloseApproval}
                 />
            </>
        )
    }
}
Approval.propTypes = propTypes;
Approval.defaultProps = defaultProps;

export const ApprovalButton = connect(state => state.ClientApplyMonitorSample, null)(Approval);
export const ApprovalChart = connect(state => state.ClientApplyMonitorSample, null)(Approvalchart);
